package com.code.petapp.fragment;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.net.Uri;
import android.widget.Toast;

import com.code.petapp.R;
import com.code.petapp.activity.ClaimActivity;
import com.code.petapp.activity.LoginActivity;
import com.code.petapp.activity.LostActivity;
import com.code.petapp.activity.MainActivity;
import com.code.petapp.activity.PersonActivity;
import com.code.petapp.adapter.HallClaimsAdapter;
import com.code.petapp.adapter.HallLostAdapter;
import com.code.petapp.bean.ClaimsBean;
import com.code.petapp.bean.LostBean;
import com.code.petapp.databinding.FragmentHallBinding;
import com.code.petapp.http.HttpListener;
import com.code.petapp.util.ClaimsUtils;
import com.code.petapp.util.LostUtils;
import com.code.petapp.util.SPCommonUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.annotations.Nullable;

public class HallFragment extends Fragment implements HallClaimsAdapter.OnItemClickListener, HallLostAdapter.OnItemLClickListener {
    private RecyclerView recyclerLost;//声明RecyclerView
    private RecyclerView recyclerClaims;//声明RecyclerView
    private HallClaimsAdapter hallClaimsAdapter;//声明适配器
    private HallLostAdapter hallLostAdapter;//声明适配器
    FragmentHallBinding binding;
    private List<LostBean> list1;
    private List<ClaimsBean> list2;
    private boolean isGetData = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentHallBinding.inflate(inflater, container, false);
        View view = binding.getRoot();
        binding.recLost.setVisibility(View.VISIBLE);
        binding.recClaims.setVisibility(View.GONE);
        binding.faL.setVisibility(View.VISIBLE);
        binding.faC.setVisibility(View.GONE);
        recyclerLost=binding.recLost;
        recyclerClaims=binding.recClaims;
        binding.btnL.setSelected(true);
        setButtonLost();
        setButtonFind();
        setFa();
        return view;
    }
    @Override
    public void onResume() {//每次重进页面都会刷新数据
        if (!isGetData) {
            //   这里可以做网络请求或者需要的数据刷新操作
            setLost();
            lostLoading();
            isGetData = true;
        }
        super.onResume();
    }
    @Override
    public void onPause() {
        super.onPause();
        isGetData = false;
    }

    public void setButtonLost() {
        binding.btnL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setLost();
                // 处理页面变化
                lostLoading();

            }
        });
    }
    private void setLost(){
        binding.btnL.setSelected(true);
        binding.btnF.setSelected(false);
        binding.recLost.setVisibility(View.VISIBLE);
        binding.recClaims.setVisibility(View.GONE);
        binding.faL.setVisibility(View.VISIBLE);
        binding.faC.setVisibility(View.GONE);
    }
    public void setButtonFind() {
        binding.btnF.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.btnF.setSelected(true);
                binding.btnL.setSelected(false);
                binding.recLost.setVisibility(View.GONE);
                binding.recClaims.setVisibility(View.VISIBLE);
                binding.faL.setVisibility(View.GONE);
                binding.faC.setVisibility(View.VISIBLE);
                // 处理页面变化
                claimsLoading();
            }
        });
    }
    private void lostLoading(){
        list1=new ArrayList<>();
        LostUtils.selectAll(SPCommonUtils.getInstance().getToken(), new HttpListener<>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void success(@Nullable ArrayList<LostBean> beanList) {
                list1.addAll(beanList);
                hallLostAdapter.notifyDataSetChanged();
            }
            @Override
            public void error(int code, @Nullable String error) {
                Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show();
            }
        });
        hallLostAdapter =new HallLostAdapter(getContext(),list1);
        hallLostAdapter.setOnItemLClickListener(this);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerLost.setLayoutManager(manager);
        recyclerLost.setAdapter(hallLostAdapter);
    }
    private void claimsLoading(){
        list2=new ArrayList<>();
        ClaimsUtils.selectAll(SPCommonUtils.getInstance().getToken(), new HttpListener<>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void success(@Nullable ArrayList<ClaimsBean> beanList) {
                list2.addAll(beanList);
                hallClaimsAdapter.notifyDataSetChanged();
            }
            @Override
            public void error(int code, @Nullable String error) {
                Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show();
            }
        });
        hallClaimsAdapter =new HallClaimsAdapter(getContext(),list2);
        hallClaimsAdapter.setOnItemClickListener(this);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerClaims.setLayoutManager(manager);
        recyclerClaims.setAdapter(hallClaimsAdapter);
    }
    private void setFa(){
        binding.faL.setOnClickListener(view -> {
            if(SPCommonUtils.getInstance().getEmail()!=""&&SPCommonUtils.getInstance().getPhone()!=""){
                startActivity(new Intent(getActivity(), LostActivity.class));
            }else{
                changeMsg();
            }
        });
        binding.faC.setOnClickListener(view -> {
            if(SPCommonUtils.getInstance().getEmail()!=""&&SPCommonUtils.getInstance().getPhone()!=""){
                startActivity(new Intent(getActivity(), ClaimActivity.class));
            }else{
                changeMsg();
            }
        });
    }

    @Override
    public void onClick(View parent,int position) {
        if(list2.get(position).getStatus().equals("待认领")) {
            String phone = list2.get(position).getPhone();
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + phone));
            startActivity(intent);
        }else{
            Toast.makeText(getActivity(), "宠物已回家，不需要联系咯~", Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public boolean onClick1(View parent,int position) {
        if(list1.get(position).getStatus().equals("走失中")) {
            String phone = list1.get(position).getPhone();
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + phone));
            startActivity(intent);
        }else{
            Toast.makeText(getActivity(), "宠物已找到，不需要联系咯~", Toast.LENGTH_SHORT).show();
        }
        return true;
    }
    private void changeMsg() {
        if(SPCommonUtils.getInstance().getEmail()==""||SPCommonUtils.getInstance().getPhone()==""){
            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setIcon(R.drawable.icon);
            builder.setTitle("身份信息不完整");
            builder.setMessage("是否前往账号管理页面填写身份信息？");
            builder.setPositiveButton("前往",
                            (DialogInterface dialog, int arg1) -> {
                                dialog.dismiss();
                                startActivity(new Intent(getActivity(), PersonActivity.class));
                            })
                    .setNegativeButton("取消",
                            (DialogInterface dialog, int arg1) -> {
                                dialog.dismiss();
                            });
            builder.create().show();
        }
    }
}